package com.servlet.web;

import com.servlet.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @Title: HelloController
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/27 9:08
 */
@WebServlet("/hello")
public class HelloController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 跳转到另一个jsp页面
        // 转发
        req.setAttribute("user","admin");
        req.getSession().setAttribute("user","adminSession");
        req.getServletContext().setAttribute("user","adminApplication");

        List<User> list = Arrays.asList(new User("小明", 18), new User("小红", 18), new User("寒梅", 18));
        req.setAttribute("list",list);
        // 重定向  url改变  请求2次  不同的request
       // resp.sendRedirect(req.getContextPath()+"/home.jsp");

        // 内部转发  url不变  请求一次  同一个request
        req.getRequestDispatcher("/home.jsp").forward(req,resp);
    }
}
